package com.chuangke.sms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.sms.dao.SMSMapper;
import com.chuangke.sms.model.SMSModel;
import com.chuangke.sms.sender.dydxwg.DydxwgSender;
import com.chuangke.sms.service.SMSService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SMSServiceImpl extends ServiceImpl<SMSMapper, SMSModel> implements SMSService {

	@Autowired
	private DydxwgSender dydxwgSender;

	@Async
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void send(SMSModel... smsModels) {
		List<SMSModel> smsModelList = dydxwgSender.send(smsModels);
		if (CollectionUtils.isEmpty(smsModelList)) {
			return ;
		}
		smsModelList.forEach(model->model.setId(IdManager.nextId()));
		this.saveBatch(smsModelList);
	}

	@Override
	public IPage<SMSModel> page(CkPageQuery page) {
		return super.page(page.getPage());
	}

}
